/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import entity.Contact;
import entity.RegisteredUser;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.LinkedList;
import java.util.List;
import javax.faces.bean.ManagedProperty;
import manager.ContactManager;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;

/**
 *
 * @author dzaric
 */
public class YahooImportBean {

	private RegisteredUser user;
	private File csvFile;
	private boolean fileReceived = false;
	private boolean importFinished = false;
	private boolean importStarted = false;

	/** Creates a new instance of YahooImportBean */
	public YahooImportBean() {
	}

	public RegisteredUser getUser() {
		return user;
	}

	public void setUser(RegisteredUser user) {
		this.user = user;
	}
	
	public boolean isFileReceived() {
		return fileReceived;
	}

	public void setFileReceived(boolean fileReceived) {
		this.fileReceived = fileReceived;
	}

	public boolean isImportFinished() {
		return importFinished;
	}

	public void setImportFinished(boolean importFinished) {
		this.importFinished = importFinished;
	}

	public File getCsvFile() {
		return csvFile;
	}

	public void setCsvFile(File csvFile) {
		this.csvFile = csvFile;
	}

	public boolean isImportStarted() {
		return importStarted;
	}

	public void setImportStarted(boolean importStarted) {
		this.importStarted = importStarted;
	}

	public void uploadListener(UploadEvent event) throws Exception {
		UploadItem item = event.getUploadItem();
		csvFile = item.getFile();
		this.fileReceived = true;
		importContacts();
	}

	public void importContacts() {
		if (fileReceived && !importStarted) {
			try {
				importStarted = true;
				System.out.println("Importing contacts");
				ContactManager cm=new ContactManager();
				for (String[] data : extractData(csvFile)) {
					System.out.println(data[0] + " " + data[1] + " " + data[2]);
					Contact newContact = new Contact();
					newContact.setRegisteredUser(user);
					String new_email = data[2];
					if (cm.contactExists(user, new_email) == false) {
						newContact.setEmail(new_email);
						newContact.setFirstName(data[0]);
						newContact.setLastName(data[1]);
						System.out.println(user.getEmail());
						newContact.getRegisteredUser().getContacts().add(newContact);
						cm.create(newContact);
					}
				}
				System.out.println("FINISHED Importing contacts");
				importFinished = true;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private List<String[]> extractData(File file) {
		List<String[]> ret = new LinkedList<String[]>();
		try {
			BufferedReader in = new BufferedReader(new FileReader(csvFile));
			in.readLine();	//throw away csv header
			String line;
			while ((line = in.readLine()) != null) {
				String[] line_data = line.split(",");
				String[] data = new String[3];
				data[0] = line_data[0].replace("\"", "");
				data[1] = line_data[2].replace("\"", "");
				data[2] = line_data[4].replace("\"", "");
				if(data[2].length()>0){
					if(data[0].length()==0){
						data[0]="N/A";
					}
					if(data[1].length()==0){
						data[1]="N/A";
					}
					ret.add(data);
				}
			}
			return ret;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
}
